<html>
<head><meta charset="utf-8"><title>Function wrappers and thunks · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html">Function wrappers and thunks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218330035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Function%20wrappers%20and%20thunks/near/218330035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html#218330035">(Nov 30 2020 at 19:01)</a>:</h4>
<p>Given that <a href="https://github.com/rust-lang/rust/pull/77688">https://github.com/rust-lang/rust/pull/77688</a> is likely to close, and I want/need something like it, what is the path forward for something like it? I'm interested in removing unneeded unsafe from the wrapper function case.</p>



<a name="218444461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Function%20wrappers%20and%20thunks/near/218444461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html#218444461">(Dec 01 2020 at 16:17)</a>:</h4>
<p>I am personally quite interested by it too. IIUC, the main reason an auto-<code>Default</code> on closures was not deemed desirable was that being able to operate on <em>values</em> of some type, <em>e.g.</em>, forging them, out of the access to its type only, especially for <em>ad-hoc</em> closures, was deemed quite dangerous, adding <code>unsafe</code> pitfalls (which, may I add, is due to the lack of <code>UnsafeFn…</code> traits and thus unsafe closures, which leads to there being many incorrectly non-<code>unsafe</code> closures (<a href="https://grep.app/search?q=%5C%7C%5Cs%2Aunsafe%20%7B&amp;regexp=true&amp;case=true&amp;filter[lang][0]=Rust">https://grep.app/search?q=%5C%7C%5Cs%2Aunsafe%20%7B&amp;regexp=true&amp;case=true&amp;filter[lang][0]=Rust</a>):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">unwrap_or_default_and_call</span><span class="o">&lt;</span><span class="n">F</span><span class="w"> </span>: <span class="nb">Default</span> <span class="o">+</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">f_opt</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">F</span><span class="o">&gt;</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f_opt</span><span class="p">.</span><span class="n">unwrap_or_default</span><span class="p">()()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// (My personal stance is that this should be an `unsafe || { … }`)</span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>::<span class="n">std</span>::<span class="n">hint</span>::<span class="n">unreachable_unchecked</span><span class="p">()</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">// capture-less closure ≃ fn item; Default?</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">None</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="n">unwrap_or_default_and_call</span><span class="p">(</span><span class="n">f</span><span class="p">);</span><span class="w"> </span><span class="c1">// Should not compile!</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<hr>
<p>So, the alternative to all this that looked the most promising was to use <code>const</code> generics:</p>
<ul>
<li>
<p>it would be a way to pass values as if within the type-system / statically dispatched (⇒ thunk usage);</p>
</li>
<li>
<p>the fact that an actual <em>value</em> would be written would help convey the idea that the callee would have access to the value.</p>
</li>
</ul>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">make_extern_c</span><span class="o">&lt;</span><span class="n">R</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="w"> </span>: <span class="nb">Fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">R</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">f</span>: <span class="nc">F</span><span class="o">&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">  </span>-&gt; <span class="nc">R</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span>: <span class="nc">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="k">fn</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">make_extern_c</span>::<span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">main</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218449971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Function%20wrappers%20and%20thunks/near/218449971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html#218449971">(Dec 01 2020 at 16:53)</a>:</h4>
<p>That looks a lot more reasonable to me. I don't see anything wrong with that.</p>



<a name="218450019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Function%20wrappers%20and%20thunks/near/218450019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html#218450019">(Dec 01 2020 at 16:53)</a>:</h4>
<p>You're actually passing a <em>value</em>, not passing a <em>type</em> and synthesizing a value.</p>



<a name="218561414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Function%20wrappers%20and%20thunks/near/218561414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks.html#218561414">(Dec 02 2020 at 14:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/213817-t-lang/topic/Function.20wrappers.20and.20thunks/near/218449971">said</a>:</p>
<blockquote>
<p>That looks a lot more reasonable to me. I don't see anything wrong with that.</p>
</blockquote>
<p>I don't either, provided we can get it sooner rather than later <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>